#include "Text.h"
#include "graphicsmanager.h"

using std::string;
using namespace EPOC;

Text::Text(GraphicsManager *pGraphicsManager)
{
	SetClassID(kCLASSID_TEXT);

	m_pGraphicsManager = pGraphicsManager;

	m_nFontSize = DEFAULT_FONT_SIZE;
	m_rTextColor.m_nRed = m_rTextColor.m_nGreen = m_rTextColor.m_nBlue = 255;
	m_pFont = NULL;
	m_sFilePath.clear();
	SetPivotCentered(false);

	m_pRebuildTexture = false;
}

Text::~Text()
{
	Text::Cleanup();
}

void Text::Cleanup()
{
	if(m_pFont)
		TTF_CloseFont(m_pFont);
	m_pFont = NULL;

	m_nFontSize = DEFAULT_FONT_SIZE;

	if(GetTexture())
	{
		GetTexture()->Release();
		SetTexture(NULL);
	}
}

void Text::SetText(string sText)
{
	if(sText.compare(m_sText) != 0)
	{
		m_sText = sText;
		m_pRebuildTexture = true;
	}
}

void Text::ClearText()
{
	m_sText.clear();
	m_pRebuildTexture = true;
}

void Text::RenderSDL(TextureSDL *pDestTexture)
{
	if(m_pRebuildTexture)
	{
		if(GetTexture())
		{
			//GetTexture()->Release();
			m_pGraphicsManager->DestroyTexture(GetTexture());
			SetTexture(NULL);
		}
				
		SDL_Surface *pSurface = RenderTextToSurface();
		if(!pSurface)
			return;
		
		Texture *pTexture = m_pGraphicsManager->CreateTexture("text", true);
		if(pTexture && pTexture->IsClass(kCLASSID_TEXTURE_SDL))
		{
			TextureSDL *pTextureSDL = (TextureSDL*)pTexture;
			pTextureSDL->SetSDLSurface(pSurface);
			pTextureSDL->SetDimension(pSurface->h, pSurface->w);
		}

		/*TextureSDL *pTexture = new TextureSDL();		
		if(pTexture->Initialize("Text", pSurface, true) == false)
		{
			pTexture->Release();
			pTexture = NULL;

			fprintf(stderr, "Error - Text::Render: '%s' could not be rendered: %s\n", m_sText.c_str(), TTF_GetError());
		}*/

		SetTexture(pTexture);
		m_pRebuildTexture = false;
	}

	GraphicsObject::RenderSDL(pDestTexture);
}

void Text::RenderOGL()
{
	if(m_pRebuildTexture)
	{
		SDL_Surface *pSurface = RenderTextToSurface();
		if(!pSurface)
			return;
				
		if(GetTexture())													// If texture already exists, just re-build it
			((TextureOGL*)GetTexture())->BuildGLTexture(pSurface);
		else																// If no texture exists... create one
		{
			Texture *pTexture = m_pGraphicsManager->CreateTexture("text", true);
			if(pTexture && pTexture->IsClass(kCLASSID_TEXTURE_OGL))
			{
				TextureOGL *pTextureOGL = (TextureOGL*)pTexture;
				pTextureOGL->BuildGLTexture(pSurface);
				

				//TextureOGL *pTexture = new TextureOGL();		
				//if(pTexture->Initialize("", pSurface, true) == false)
				//{
				//	pTexture->Release();
				//	pTexture = NULL;
				//}
				
				SetTexture(pTextureOGL);
			}
		}

		m_pRebuildTexture = false;
	}

	GraphicsObject::RenderOGL();
}

SDL_Surface* Text::RenderTextToSurface()
{
	SDL_Color rColor;
	rColor.r = m_rTextColor.m_nRed;
	rColor.g = m_rTextColor.m_nGreen;
	rColor.b = m_rTextColor.m_nBlue;

	SDL_Surface *pSurface = TTF_RenderText_Blended(m_pFont, m_sText.c_str(), rColor);
	if(!pSurface)
		fprintf(stderr, "Error - Text::Render: '%s' could not be rendered: %s\n", m_sText.c_str(), TTF_GetError());

	return pSurface;
}

bool Text::Load(string sFontPath, int nFontSize)
{
	m_sFilePath = sFontPath;
	m_nFontSize = nFontSize;

	m_pFont = TTF_OpenFont(m_sFilePath.c_str(), m_nFontSize);
	if(!m_pFont) 
	{
		printf("Error - Text::LoadFont - TTF_OpenFont: %s\n", TTF_GetError());
		return false;
	}
	
	return true;
}